Imports DotNetNuke
Imports System.Web.UI
Imports System.Xml
Imports System.Text
Imports System.Net
Imports System.Collections.Generic
Imports System.Reflection
Imports DotNetNuke.Security



Namespace YourCompany.Modules.TwitterDNN

    Partial Class ViewTwitterDNN
        Inherits DotNetNuke.Entities.Modules.PortalModuleBase


        Function fixNull(ByVal sItem) As String
            If sItem Is System.DBNull.Value Then
                fixNull = "0"
            Else
                fixNull = sItem
            End If
        End Function

        Function fParseTweet(ByVal sTweet As String) As String
            'probably a better way to do this w/ regex, but this will be a good programming exercise

            Dim tmpOut As String = ""
            Dim twtArray As Array = sTweet.Split(" ")
            Dim x As String

            If twtArray.Length > 0 Then
                For Each x In twtArray
                    '080822 - busse - added length checking to prevent symbol+space from getting linked
                    If Left(x, 1) = "@" And x.Length > 1 Then
                        tmpOut = tmpOut & " <a href='http://twitter.com/" & x.Remove(0, 1) & "'>" & x & "</a>"
                    ElseIf Left(x, 1) = "#" And x.Length > 1 Then
                        tmpOut = tmpOut & " <a href='http://search.twitter.com/search?q=" & x.Remove(0, 1) & "'>" & x & "</a>"
                    ElseIf Left(x, 4) = "http" And x.Length > 4 Then
                        tmpOut = tmpOut & " <a href='" & x & "'>" & x & "</a>"
                    Else
                        tmpOut = tmpOut & " " & x
                    End If

                Next
            End If

            fParseTweet = tmpOut
        End Function


        'special thanks to Alan Le's Witty: http://blogs.vertigo.com/personal/alanl/Blog/archive/2007/11/21/witty-creating-a-net-twitter-api.aspx
        '  for helping me upgrade my HTTP and XML requesting and parsing skills from my old Msxml2.XMLHTTP.3.0 and MSXML2.DOMDocument.3.0 ways

        Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim varFeedData As String = "<UL class='TwitterDNNUL'>"

            'xx-change: need some error handling for when this is blank
            Dim url As String = "http://twitter.com/statuses/user_timeline/" & Settings("yourname") & ".xml"
            
            Dim request As HttpWebRequest = WebRequest.Create(url)
            Dim response As HttpWebResponse = request.GetResponse

            'If status = 200 (OK), then load it up
            If response.StatusCode = HttpStatusCode.OK Then
                Dim reader As New IO.StreamReader(response.GetResponseStream())
                Dim doc As New XmlDocument()
                doc.Load(reader)

                Dim nodes As XmlNodeList = doc.SelectNodes("/statuses/status")

                Dim node As XmlNode
                Dim tmpCount As Integer = 0

                For Each node In nodes
                    tmpCount = tmpCount + 1
                    varFeedData = varFeedData & "<LI class='TwitterDNNLI" & tmpCount & "'>" & fParseTweet(node.SelectSingleNode("text").InnerText) & " <a href='http://twitter.com/" & Settings("yourname") & "/statuses/" & node.SelectSingleNode("id").InnerText & "'>" & Settings("linktweet") & "</a></LI>" & vbCrLf
                    If tmpCount = CInt(Settings("numtweets")) Then
                        Exit For
                    End If
                Next

                reader.Close()
                reader = Nothing
            Else
                'status is not 200(OK), show Fail Whale Msg
                varFeedData = varFeedData & "<LI class='TwitterDNNErr'>" & Settings("failwhale") & "</LI>" & vbCrLf

            End If
            divTwitter.InnerHtml = varFeedData & "</UL>"

        End Sub


    End Class

End Namespace